package com.tengju.support.infrastructure.repository.message.convert;

import com.tengju.support.application.shared.ApplicationException;
import com.tengju.support.application.shared.ApplicationExceptionCode;
import com.tengju.support.domain.model.message.*;
import com.tengju.support.domain.model.user.UserId;
import com.tengju.support.domain.shared.JsonUtil;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.AppMessageCentreDetailDO;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.AppMessageCentreUnreadCountDO;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.SupportMessageDO;
import org.apache.commons.collections.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class MessageBatchRelationConvert {

    public static SupportMessageDO convertToDO(Message message) {
        if (message == null) {
            return null;
        }
        SupportMessageDO supportMessageDO = new SupportMessageDO();
        supportMessageDO.setMessageType(message.getMessageType().getVal());
        supportMessageDO.setTaskId(message.getTaskId().getValue());
        supportMessageDO.setBatchId(message.getBatchId().toString());
        supportMessageDO.setReceiveUserId(message.getUserId().getValue());
        supportMessageDO.setReadStatus(message.getReadStatus().getVal());
        supportMessageDO.setSendStatus(message.getSendStatus().getVal());
        supportMessageDO.setSendTime(message.getSendTime());
        supportMessageDO.setExtra(JsonUtil.toJSONString(message.getExtra()));
        supportMessageDO.setUpdateTime(LocalDateTime.now());
        supportMessageDO.setCreateTime(LocalDateTime.now());
        supportMessageDO.setTitle(message.getTitle());
        supportMessageDO.setImgUrl(message.getImgUrl());
        supportMessageDO.setContent(message.getContent());
        supportMessageDO.setJumpUrl(message.getJumpUrl());


        return supportMessageDO;
    }


    public static Message convertToModel(SupportMessageDO supportMessageDO) {
        if (supportMessageDO == null) {
            return null;
        }

        MessageExtra extra =
                JsonUtil.toObject(supportMessageDO.getExtra(), MessageExtra.class);
        BatchId batchId = BatchIdFactory.create(supportMessageDO.getBatchId());

        return new Message(new TaskId(supportMessageDO.getTaskId()),
                MessageType.fromVal(supportMessageDO.getMessageType()),
                batchId,
                new UserId(supportMessageDO.getReceiveUserId()),
                ReadStatus.fromVal(supportMessageDO.getReadStatus()),
                SendStatus.fromVal(supportMessageDO.getSendStatus()),
                supportMessageDO.getSendTime(),
                extra,
                supportMessageDO.getTitle(),
                supportMessageDO.getImgUrl(),
                supportMessageDO.getContent(),
                supportMessageDO.getJumpUrl());
    }

    public static List<AppMessageCentreUnreadCount> toAppMessageCentreUnreadCountModelList(
            List<AppMessageCentreUnreadCountDO> appMessageCentreUnreadCountDOList) {

        List<AppMessageCentreUnreadCount> appMessageCentreUnreadCountList = new ArrayList<>();

        if (CollectionUtils.isEmpty(appMessageCentreUnreadCountDOList)) {
            return appMessageCentreUnreadCountList;
        }

        appMessageCentreUnreadCountDOList.forEach(v -> {
            AppMessageCentreUnreadCount model = new AppMessageCentreUnreadCount(MessageType.fromVal(v.getMessageType()),
                    v.getUnreadMessageCount() == null ? Long.valueOf(0) : v.getUnreadMessageCount());

            appMessageCentreUnreadCountList.add(model);
        });

        return appMessageCentreUnreadCountList;

    }

    public static AppMessageCentreDetail toAppMessageCentreDetailModel(AppMessageCentreDetailDO detailDO) {
        if (detailDO == null) {
            return null;
        }
        MessageType messageType = MessageType.fromVal(detailDO.getMessageType());
        if (messageType == null) {
            throw ApplicationException.error(ApplicationExceptionCode.MESSAGE_TYPE_NOT_EXIST,detailDO.getMessageType());
        }

        return new AppMessageCentreDetail(messageType,
                detailDO.getTitle(),detailDO.getSendTime());
    }

}
